home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / SimplePrintout / README.TXT < prev    next >
Encoding:
Text File  |  2004-10-22  |  12.7 KB  |  363 lines

  1. README.TXT
  2. ==========
  3.  
  4.  
  5. What is SimplePrintout?
  6. =======================
  7.  
  8. This directory contains a very simple printing class.
  9.  
  10. SimplePrintout unit provides a very basic printout for your
  11. quick and dirty Delphi for .NET projects. Many times when
  12. you sit down to write a tiny software project you feel like
  13. there is no easy way of printing out relatively small amounts
  14. of data.
  15.  
  16. Of course, .NET makes it easier to print text and pictures. However
  17. the first time you need to print justified text or bordered text, or
  18. pages with simple header or footer, you come to realize that you
  19. still have to revert to counting pixels.
  20.  
  21. In many cases using a professional report generating tools
  22. may be an overkill, but writing your own printing component
  23. can be a daunting task too.
  24.  
  25. That is where SimplePrintout class comes into play.
  26.  
  27. SimplePrintout keeps all those details hidden from you. Although
  28. it is not a full-blown reporting tool, you will be amazed just
  29. how much you can accomplish with this little unit...
  30.  
  31.  
  32.  
  33. Installation
  34. ============
  35.  
  36. At the time of this writing SimplePrintout for Delphi for .NET
  37. has some minor problems with code serialization. I need to clean
  38. that up to make it into a component. So for now SimplePrintout
  39. is provided as a Delphi unit, rather than an installable component.
  40. As a result, it does not require any installation. Simply copy the
  41. unit called Simp_Prn.PAS to your project directory and add
  42. "Simp_Prn" to your unit's USES statement.
  43.  
  44. Don't forget to check Borland CodeCentral for the latest code
  45. updates.
  46.  
  47.  
  48.  
  49. How to use SimplePrintout component?
  50. ====================================
  51.  
  52. The best way to find out about SimplePrintout is to compile and
  53. run the DemoProj application. This demo application will get
  54. you started.
  55.  
  56.  
  57. Lesson 1.
  58. --------
  59.   You may simply create and use an instance of SimplePrintout at
  60.   runtime. It is relatively easy. Start by adding "Simp_Prn" unit
  61.   to your "uses" stament.
  62.  
  63.     procedure TWinForm.Button1_Click(sender: System.Object;
  64.                                      e: System.EventArgs);
  65.     VAR p: SimplePrintout;
  66.         s: StreamReader;
  67.     begin
  68.       // It's very simple to create
  69.       // a SimplePrintout...
  70.       //
  71.       p := SimplePrintout.Create();
  72.  
  73.       try
  74.         // Always call BeginPrintout!
  75.         //
  76.         p.BeginPrintout();
  77.  
  78.         // And finally here is how to print out a text file...
  79.         // This is much simpler than Microsoft .NET sample.
  80.         //
  81.         s := StreamReader.Create('c:\autoexec.bat');
  82.         p.WriteString(s.ReadToEnd());
  83.         s.Close();
  84.  
  85.         // Use this call to flush the text to the printer
  86.         //
  87.         p.EndPrintout();
  88.  
  89.         // NOTE: To cancel printout you may use
  90.         //   p.AbortPrintout(); instead of p.EndPrintout();
  91.         //
  92.       except
  93.         on E: Exception do
  94.           MessageBox.Show(E.Message);
  95.       end;
  96.       p.Free; // with .NET you do not really need this one
  97.     end;
  98.  
  99.  
  100. Lesson 2.
  101. --------
  102. Let us add some meat to our sample code...
  103.  
  104.     procedure TWinForm.Button1_Click(sender: System.Object;
  105.                                      e: System.EventArgs);
  106.     VAR p: SimplePrintout;
  107.         s: StreamReader;
  108.     begin
  109.       p := SimplePrintout.Create();
  110.  
  111.       try
  112.         // Always call BeginPrintout!
  113.         //
  114.         p.BeginPrintout();
  115.  
  116.         // You may set margins (measured in inches)
  117.         //
  118.         p.TopMargin := 1.5; // leave some room for the header
  119.         p.BottomMargin := 1; // leave some room for the footer
  120.  
  121.         // It is pretty easy to set footers and headers...
  122.         //
  123.         p.Header.Text := 'you can have'+#13#10+'multi-line HEADERS';
  124.         p.Footer.TextIndentBottom := 0.3;
  125.         p.Footer.Text := 'Footer text';
  126.  
  127.         // To align your paragraph text to Left,
  128.         // simply set Band.TextAlignment to baLeft
  129.         // (Your choices for alignment are:
  130.         //  baLeft, baRight, baCentered, and baJustified
  131.         //
  132.         p.Band.TextAlignment := BandAlignment.baLeft;
  133.  
  134.         // This is how you can change fonts.
  135.         // It's pretty striaghtforward.
  136.         //
  137.         p.Band.TextFont := System.Drawing.Font.Create('Arial', 12);
  138.  
  139.         // And finally here is how to print out a text file...
  140.         // This is much simpler than Microsoft .NET sample.
  141.         //
  142.         s := StreamReader.Create('c:\autoexec.bat');
  143.         p.WriteString(s.ReadToEnd());
  144.         s.Close();
  145.  
  146.         // Use this call to flush the text to the printer
  147.         //
  148.         p.EndPrintout();
  149.  
  150.         // NOTE: To cancel printout you may use
  151.         //   p.AbortPrintout(); instead of p.EndPrintout();
  152.         //
  153.       except
  154.         on E: Exception do
  155.           MessageBox.Show(E.Message);
  156.       end;
  157.       p.Free; // with .NET you do not really need this one
  158.     end;
  159.  
  160. Lesson 3.
  161. --------
  162.   How to add Page numbering to the footer (or header). Or rather,
  163.   let us paraphrase this question. How to make headers and footers
  164.   more dynamic?
  165.  
  166.   All you need is to add an event handler like this:
  167.  
  168.     procedure TWinForm.on_PrintPage(sender: System.Object;
  169.                               ua: UserPrintPageEventArgs);
  170.     begin
  171.       (sender as SimplePrintout).Footer.Text :=
  172.         'Page ' + ua.PageNo.ToString;
  173.     end;
  174.  
  175.     procedure TWinForm.Button1_Click(sender: System.Object;
  176.                                      e: System.EventArgs);
  177.     VAR p: SimplePrintout;
  178.         s: StreamReader;
  179.     begin
  180.       p := SimplePrintout.Create();
  181.  
  182.       try
  183.         // Always call BeginPrintout!
  184.         //
  185.         p.BeginPrintout();
  186.  
  187.         // Add our print page handler here
  188.         //
  189.         Include(p.PrintPage, on_PrintPage);
  190.  
  191.         s := StreamReader.Create('c:\autoexec.bat');
  192.         p.WriteString(s.ReadToEnd());
  193.         s.Close();
  194.  
  195.         p.EndPrintout();
  196.       except
  197.         on E: Exception do
  198.           MessageBox.Show(E.Message);
  199.       end;
  200.     end;
  201.  
  202.  
  203. Lesson 4.
  204. --------
  205.   We already know some methods and some properties of the SimplePrintout
  206.   class. But there are other methods and properties you may want to use
  207.   as you go.
  208.  
  209.      BeginPrintout  - use this call before calling any other methods.
  210.  
  211.      WriteString    - lets you print one string at the time. The string
  212.                       can be as long as you want it to be. It will be
  213.                       wrapped as necessary. This way you can put your
  214.                       entire document into one string and send it to
  215.                       printer with one command.
  216.  
  217.      PageBreak      - inserts a page break.
  218.  
  219.      EndPrintout    - this will flush data to the physical printer.
  220.  
  221.      AbortPrintout  - this will terminate output without printing
  222.                       to physical device, if called instead of EndPrintout.
  223.  
  224.      Page setup:
  225.        NonPrintableGutterSize - measured in inches.
  226.                       Most printers do not let you print at
  227.                       the very edge of the page. We set this
  228.                       non-printable gutter size to 3/16" by default
  229.                       The programmer can change this setting.
  230.  
  231.        TopMargin -    Top margin including the gutter, in inches
  232.        LeftMargin -   Left margin including the gutter, in inches
  233.        RightMargin -  Right margin including the gutter, in inches
  234.        BottomMargin - Bottom margin including the gutter, in inches
  235.  
  236.        HeaderText -   Static text for the page header, this text
  237.                       can be changed dynamically in an event handler
  238.  
  239.        FooterText -   Static text for the page footer, this text
  240.                       can be changed dynamically in an event handler
  241.  
  242.        There are two areas on the page. One is within the page margins
  243.        and another is the page including the margin areas. For the lack
  244.        of a better term, these two areas are called Inner and Outer:
  245.  
  246.          InnerBorderPixels - measured in pixels
  247.                       Thickness of the border inside the margins, in pixels
  248.  
  249.          OuterBorderPixels - measured in pixels
  250.                       Thickness of the border outside the margins, but still
  251.                       within the non-printable gutter area.
  252.  
  253.          InnerBorderColor - Color of the border inside the margins
  254.          OuterBorderColor - Color of the border outside the margins
  255.          InnerBackColor - Background color of the page inside the margins
  256.          OuterBackColor - Background color of the margins
  257.  
  258.   Header, Footer, Band properties:
  259.  
  260.    First of all you probably realize that there is a property called
  261.    Footer. Then there is a Header. There is also a property called
  262.    "Band." All of these properties are of type TBand. TBand represents
  263.    a paragraph of text with its own set of parameters.
  264.  
  265.    Header and Footer are probably easy to understand. The Band property
  266.    simply means current paragraph. You can set up the current paragraph
  267.    font, size, etc., and simply call WriteString(text). The text will
  268.    be printed with current settings. Then you change the Band again,
  269.    call WriteString() and the text will be printed with new paragraph
  270.    settings.
  271.  
  272.    So as you see there are three paragraphs you work with: Header, Footer,
  273.    and the current paragraph exposed through the Band property.
  274.  
  275.    A paragraph (or band) has the following properties:
  276.  
  277.          TextAlignment - current text alignment
  278.                       instructs the simple printout component to switch
  279.                       paragraph alignment. This property can be changed
  280.                       in the middle of printing, it will affect subsequent
  281.                       WriteString() calls.
  282.                       This property can be set to: baRight, baCentered,
  283.                       baJustified, and default: baLeft.
  284.  
  285.          TextFont   - lets you select and change a font for the body of
  286.                       the paragraph.
  287.  
  288.       Keep in mind that a paragraph usually consists of a box
  289.       (background) and the text (foreground). Box  and text can
  290.       have their own indents, colors, etc:
  291.  
  292.          BorderPixelSize - paragraph [box] border pixel size
  293.                       By default this is set to 0, so no box is not
  294.                       printed. But the box is still there. It has
  295.                       a white background color. You can get rid of the
  296.                       box by setting Transparent property to TRUE
  297.  
  298.          Transparent - we just explained this one
  299.  
  300.          BorderColor - paragraph [box] border color
  301.          ForeColor   - text color
  302.          BackColor   - paragraph [box] color, background color
  303.  
  304.          IndentLeft  - paragraph indents, in inches
  305.          IndentTop
  306.          IndentRight
  307.          IndentBottom
  308.  
  309.          TextIndentLeft - text indents within the paragraph
  310.          TextIndentRight
  311.          TextIndentTop
  312.          TextIndentBottom
  313.  
  314. Lesson 5.
  315. --------
  316.   SimplePrintout also has a couple of Event handlers. Please
  317.   take a look at the DemoProj application to see how they are used.
  318.  
  319.   PrintPage event is of type UserPrintPageEvent. It is very similar to
  320.   .NET PrintPage event, so that the programmer gets the full control
  321.   before printing each page. Pay attention to the second parameter
  322.   of type UserPrintPageEventArgs. It has these members:
  323.  
  324.     PageParms: PrintPageEventArgs;
  325.         -- this is the original PrintPageEventArgs, we
  326.            pass it to the user.
  327.  
  328.     PageNo: Integer;  -- (read only) current page count
  329.  
  330.     The following five Boolean members are set to FALSE by default.
  331.     However the programmer can set them to TRUE in the event handler
  332.     to indicate specific behaviors:
  333.  
  334.     PrintoutLastPage: Boolean;
  335.         -- User may set this to TRUE, which means that SimplePrintout
  336.            will print this page and then stop printing.
  337.  
  338.     BackgroundHandledByUser: Boolean;
  339.         -- If set by user to TRUE it means that SimplePrintout does not
  340.            have to paint the page background, user may have added a
  341.            background picture, for example.
  342.  
  343.     PageHandledByUser: Boolean;
  344.         -- (set by user) SimplePrintout does not paint the current
  345.            page user handled the page. Note that it still paints the
  346.            header and footer. The next two items let you disable the
  347.            header or the footer, or both.
  348.  
  349.     HeaderHandledByUser: Boolean; -- indicates that user handled the header
  350.     FooterHandledByUser: Boolean; -- indicates that user handled the header
  351.  
  352. ----------------------------------------------------
  353. Please take a look at the demo project to see the working code. You
  354. may find it easier to cut and paste the code into your project and
  355. build it up from there.
  356.  
  357. ----------------------------------------------------
  358. Enjoy!
  359.  
  360. Feedback to: alfred@softsci.com
  361. Ref: SimplePrintout for Delphi for .NET (11/19/03)
  362. ----------------------------------------------------
  363.